// JavaScript Document
function get_value(el) {
    var data = el.getAttribute('value');
    var giatri = "form[name='" + data + "']";
    var $quantity = $(giatri).find('#quantity_product').val();
    var $key = $(giatri).find('#key').val();
    var $product_id = $(giatri).find('#product_id').val();
    var class_tr = "table tr[class='product_row_" + data + "']";
    var class_price = "table tr td[class='wpsc_product_price_" + data + "']";
    // các biến của upfate quantity khi no bang 0
    //   var $quantity=0;
    var $key = $(giatri).find('#key').val();
    var $product_id = $(giatri).find('#product_id').val();
    var class_tr = "table tr[class='product_row_" + data + "']";
    var class_price = "table tr td[class='wpsc_product_price_" + data + "']";
    $.ajax({
        url: "../markonline/wp-admin/admin-ajax.php",
        type: 'POST',
        data: 'action=update_quantity01&quantity=' + $quantity + '&key=' + $key + '&product_id=' + $product_id,
        success: function(results)
        {
            update_cart_top();
            update_cart_top_value();
            var data = $.parseJSON(results);
            if (data.remove == '1') {
                $(class_tr).hide();
                //$("span[id='price']").html(data.sum_total_price);
                $("span[class='grandtotal']").html(data.sum_total_price);
                // update key value
                var count = $("input[name='count']").val();
                $key++;
                //alert($key)
                for (var i = $key; i < count; i++)
                {
                    var form_name_update = "form[name='update_quatily_" + i + "']";
                    var key = $(form_name_update).find('#key').val();
                    $(form_name_update).find('#key').val(key - 1);
                    //alert(key);
                }
            } else {
                $(class_price).html(data.total_price);
                $("span[class='grandtotal']").html(data.sum_total_price);
            }
        }
    });
}
function get_value_remove(el) {
    var answer = confirm("Are you remove product?");
    if (!answer)
        return false;
    var data = el.getAttribute('value');
    var giatri = "form[name='" + data + "']";
    var $quantity = 0;
    var $key = $(giatri).find('#key').val();
    var $product_id = $(giatri).find('#product_id').val();
    var class_tr = "table tr[class='product_row_" + data + "']";
    var class_price = "table tr td[class='wpsc_product_price_" + data + "']";
    $.ajax({
        url: "../markonline/wp-admin/admin-ajax.php",
        type: 'POST',
        data: 'action=update_quantity01&quantity=' + $quantity + '&key=' + $key + '&product_id=' + $product_id,
        success: function(results)
        {
            update_cart_top();
            update_cart_top_value();
            var data = $.parseJSON(results);
            if (data.remove == '1') {
                $(class_tr).hide();
                $("span[class='grandtotal']").html(data.sum_total_price);

            } else {
                $(class_price).html(data.total_price);
            }
            // update key value
            var count = $("input[name='count']").val();
            $key++;

            //alert($key)
            for (var i = $key; i < count; i++)
            {
                var form_name_update = "form[name='update_quatily_" + i + "']";
                var key = $(form_name_update).find('#key').val();
                $(form_name_update).find('#key').val(key - 1);
                //alert(key);
            }
        }
    });
}
function click_addtocart(data)
{
    var input_click = "#product_" + data + "_submit_button";
    $(input_click).click();
    update_cart_top();
    update_cart_top_value();
    //alert('them vao gio hang');
}
function update_cart_top()
{
    $.ajax({
        url: "../markonline/wp-admin/admin-ajax.php",
        type: 'POST',
        data: 'action=update_cart_top',
        success: function(results)
        {
            //alert(results);
            $("#cart-link").html(results);
        }
    });
}
function update_cart_top_value()
{
    $.ajax({
        url: "../markonline/wp-admin/admin-ajax.php",
        type: 'POST',
        data: 'action=update_cart_table_top',
        success: function(results)
        {
            //alert(results);
            //alert(results);
            $("#cart-panel").html(results);
        }
    });
}
function click_checkout()
{
    //var input_click="#product_"+data+"_submit_button";
    //alert('add to cart complet');
    alert('mua hÃ ng');
    $('#buy_product_complete').click();
//		 update_cart_top();
//		 update_cart_top_value();
}